package com.example.security.config;

import com.alibaba.fastjson2.JSON;
import com.example.security.domain.User;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: DongGuoZhen
 * @Date: 2025/06/18/11:07
 * @Description:
 */

//认证成功处理器
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {

        Object principal = authentication.getPrincipal(); // 获取用户身份信息
//        Object credentials = authentication.getCredentials(); // 获取用户凭证信息
//        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities(); // 获取用户权限信息

        HashMap result = new HashMap();
        result.put("code",0);
        result.put("message","登录成功");
        result.put("data",principal); //
        String json = JSON.toJSONString(result);
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().write(json);
        response.getWriter().flush();
        response.getWriter().close();
    }
}
